package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.*;
import com.xinhe.web.reposity.YunChaSeedAssetCountRepository;
import com.xinhe.web.response.TeamBuyOrderView;
import com.xinhe.web.util.Snowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.math.BigDecimal;

@Service
public class YunChaSeedAssetCountService {
    @Autowired
    private YunChaSeedAssetCountRepository yunChaSeedAssetCountRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public QueryResults<YunChaSeedAssetCount> pageYunChaSeedAssetCount(int pageNum, int pageSize) {
        QYunChaSeedAssetCount qYunChaSeedAssetCount = QYunChaSeedAssetCount.yunChaSeedAssetCount;
        return queryFactory.select(
                        Projections.bean(
                                YunChaSeedAssetCount.class,
                                qYunChaSeedAssetCount.id,
                                qYunChaSeedAssetCount.balance,
                                qYunChaSeedAssetCount.configurationDateId,
                                qYunChaSeedAssetCount.userId
                        )
                ).from(qYunChaSeedAssetCount)
                .offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }

    public Result delete(Long yunChaSeedAssetCountId) {
        yunChaSeedAssetCountRepository.deleteById(yunChaSeedAssetCountId);
        return Result.ok();
    }

    public Result<YunChaSeedAssetCount> findById(Long yunChaSeedAssetCountId) {
        return Result.ok(yunChaSeedAssetCountRepository.findById(yunChaSeedAssetCountId).get());
    }

    public Result saveModel(Long configurationDateId, Long userId, BigDecimal balance, String remark) {
        YunChaSeedAssetCount yunChaSeedAssetCount = new YunChaSeedAssetCount();
        yunChaSeedAssetCount.setId(Snowflake.generateId());
        yunChaSeedAssetCount.setBalance(balance);
        yunChaSeedAssetCount.setUserId(userId);
        yunChaSeedAssetCount.setConfigurationDateId(configurationDateId);
        yunChaSeedAssetCountRepository.save(yunChaSeedAssetCount);
        return Result.ok();
    }
}
